import Vue from "vue";

/*
  getYearArr, startYear 开始年份 参数为空时为当前年份前5年 endYear 结束年份 参数为空时为当前年份
  getNowFormatDate,获取当前格式化后的时间日期
  inTimePeriod,只能比较同一天之内的时间，不跨天比较
  formatTime,时间格式化。可以作为过滤器直接使用
  getCurrentYearMonthDay,获取今天的 年月日
  getCurrentWeekday, 获取今天是星期几
  getYearWeek,获取 某年某月某日 是在 那一年 的第几周
  getMonthWeek, 获取 某年某月某日 是在 那一月 的第几周
  getCurrentWeekNumber, 获取当前是开始日期之后的第几周
  getDatesAfterWeeks, 获取 开始日期 之后 第n周 的 日期
  getWeekDatesForYMD,  获取某一天所在周的日期
  getDayWeekday,  获取某一天(年月日)是星期几
  getWeekDates,获取当前周的日期
  getMonthDays, 获取某年的某月共多少天
  timeStrToTimestamp, 时间字符串（如2020-08-19 10:09）转时间戳
  getPastDatetime, 获得过去的时间,time的单位为秒
  getDateTimeNow,获取当前时间
  timestampToTime,时间戳转换为时间
  timeNow,几分钟，几小时，刚刚，几天，几月，几年
*/
// 几分钟，几小时，刚刚，几天，几月，几年


function timeNow(updateTime) {
  if (updateTime === null) {
    return "";
  }
  let now = new Date().getTime();
  let second = Math.floor((now - updateTime) / 1000);
  let minute = Math.floor(second / 60);
  let hour = Math.floor(minute / 60);
  let day = Math.floor(hour / 24);
  let month = Math.floor(day / 31);
  let year = Math.floor(month / 12);

  console.log(hour);
  console.log(minute);
  if (year > 0) {
    return year + "年前";
  } else if (month > 0) {
    return month + "月前";
  } else if (day > 0) {
    let ret = day + "天前";
    if (day >= 7 && day < 14) {
      ret = "1周前";
    } else if (day >= 14 && day < 21) {
      ret = "2周前";
    } else if (day >= 21 && day < 28) {
      ret = "3周前";
    } else if (day >= 28 && day < 31) {
      ret = "4周前";
    }
    return ret;
  } else if (hour > 0) {
    return hour + "小时前";
  } else if (minute > 0) {
    return minute + "分钟前";
  } else if (second > 0) {
    return second + "秒前";
  } else {
    return "刚刚";
  }
}

function dateformat(date) {
  var time = new Date(date);
  var day = ("0" + time.getDate()).slice(-2);
  var month = ("0" + (time.getMonth() + 1)).slice(-2);

  var hour = ("0" + time.getHours()).slice(-2);
  var minute = ("0" + time.getMinutes()).slice(-2);
  var second = ("0" + time.getSeconds()).slice(-2);

  // var today = time.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  var today = time.getFullYear() + "-" + month + "-" + day;
  return today;
}
//时间戳转换为时间
function timestampToTime(timestamp, number) {
  var date = new Date(timestamp);
  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + "-";
  var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
  var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
  var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

  var res = Y + M + D + h + m + s;
  if (number) {
    return res.substring(0, number);
  }
  return res;
}
//获取当前时间
function getDateTimeNow() {
  var time = new Date();
  var day = ("0" + time.getDate()).slice(-2);
  var month = ("0" + (time.getMonth() + 1)).slice(-2);

  var hour = ("0" + time.getHours()).slice(-2);
  var minute = ("0" + time.getMinutes()).slice(-2);
  var second = ("0" + time.getSeconds()).slice(-2);

  var today = time.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  return today;
}
//获取日期时间,time为Date对象
function getDatetimeByDateObj(time) {
  var day = ("0" + time.getDate()).slice(-2);
  var month = ("0" + (time.getMonth() + 1)).slice(-2);

  var hour = ("0" + time.getHours()).slice(-2);
  var minute = ("0" + time.getMinutes()).slice(-2);
  var second = ("0" + time.getSeconds()).slice(-2);

  var datetime = time.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  return datetime;
}
//获得过去的时间,time的单位为秒
function getPastDatetime(time) {
  var curDate = new Date().getTime();
  time *= 1000;
  var pastResult = curDate - time;
  return getDatetimeByDateObj(new Date(pastResult));
}
//时间字符串（如2020-08-19 10:09）转时间戳
function timeStrToTimestamp(timeStr) {
  return new Date(timeStr).getTime() / 1000;
}
// 获取某年的某月共多少天
function getMonthDays(year, month) {
  return new Date(year, month, 0).getDate();
}

// 获取某一天(年月日)是星期几
function getDayWeekday(year, month, date) {
  const dateNow = new Date(year, parseInt(month) - 1, date);
  // 0-6, 0 is sunday
  return dateNow.getDay();
}

// 获取某一天所在周的日期
function getWeekDatesForDate(date) {
  const timeStamp = date.getTime();
  const currentDay = date.getDay();
  let dates = [];
  for (var i = 0; i < 7; i++) {
    const _i = i - ((currentDay + 6) % 7);
    const _isToday = _i === 0;
    const _date = new Date(timeStamp + 24 * 60 * 60 * 1000 * _i);
    dates.push({
      theDay: _isToday, // 只是指当前查询的时间，在那一周的哪一天. 并不是指查询的这一天是否是今天
      date: _date,
      today: isToday(_date), // 是否是今天
    });
  }
  return dates;
}

// 获取当前周的日期
function getWeekDates() {
  const new_Date = new Date();
  return getWeekDatesForDate(new_Date);
}

// 获取某一天所在周的日期
function getWeekDatesForYMD(year, month, date) {
  const dateNow = new Date(year, parseInt(month) - 1, date);
  return getWeekDatesForDate(dateNow);
}

// 获取 开始日期 之后 第n周 的 日期
function getDatesAfterWeeks(start, diff) {
  const _arr = start.split("-");
  const y = _arr[0];
  const m = _arr[1];
  const d = _arr[2];
  const _start = new Date(y, parseInt(m) - 1, d);
  const timeStamp = _start.getTime();
  const date = new Date(timeStamp + diff * 7 * 24 * 60 * 60 * 1000);
  return getWeekDatesForDate(date);
}

// 获取当前是开始日期之后的第几周，如果大于total，则表示已经结束过时，如果小于0，则表示start还没有到来。
// 我们这里需要注意的是，比如 开始日期是星期四（没有给出星期一的日期），下周的星期一应该是第2周，而不是还在第一周。(我们这里不仅仅只是差值周数计算，还需要受实际周的限制)
// 不能只是计算差值，如果给出的开始日期是星期一的，差值计算正确，如果不是，则需要考虑开始日期的星期
function getCurrentWeekNumber(start, total) {
  const _arr = start.split("-");
  const y = _arr[0];
  const m = _arr[1];
  const d = _arr[2];
  const _start = new Date(y, parseInt(m) - 1, d);
  let _timestamp = _start.getTime();
  let day = _start.getDay(); // 星期几
  day = (day + 6) % 7; // 将星期几转化为距离星期一多少天
  // 我们将开始时间修正到那一周的星期一
  // 这里我们将星期天作为最后一天，星期一作为第一天
  _timestamp = _timestamp - day * (24 * 60 * 60 * 1000);
  // current
  const dt = new Date();
  const _y = dt.getFullYear();
  const _m = dt.getMonth();
  const _d = dt.getDate();
  const today = new Date(_y, _m, _d);
  const todayStamp = today.getTime();
  const diff = todayStamp - _timestamp;
  if (diff < 0) {
    // start还没有开始，未来返回-1
    return -1;
  }
  const weekStamp = 7 * 24 * 60 * 60 * 1000;
  let weekDiff = parseInt(diff / weekStamp);
  const more = diff % weekStamp;
  // if (more >= 24 * 60 * 60 * 1000) {
  // weekDiff += 1
  // }
  // wo always need to plus 1 for weekDiff
  const weekNumber = weekDiff + 1;
  if (weekNumber > total) {
    // 已经过期
    return -2;
  }
  return weekNumber;
}

// 查询某日期是否是今天
function isToday(date) {
  const dt = new Date();
  const y = dt.getFullYear(); // 年
  const _y = date.getFullYear();
  const m = dt.getMonth(); // 月份从0开始的
  const _m = date.getMonth();
  const d = dt.getDate(); //日
  const _d = date.getDate();
  return _y + "-" + _m + "-" + _d === y + "-" + m + "-" + d;
}

// 获取 某年某月某日 是在 那一月 的第几周
function getMonthWeek(year, month, date) {
  /*  
	    month = 6 - w = 当前周的还有几天过完(不算今天)  
	    year + month 的和在除以7 就是当天是当前月份的第几周  
	*/
  let dateNow = new Date(year, parseInt(month) - 1, date);
  let w = dateNow.getDay(); //星期数
  let d = dateNow.getDate();
  return Math.ceil((d + 6 - w) / 7);
}

// 获取 某年某月某日 是在 那一年 的第几周
function getYearWeek(year, month, date) {
  /*  
	    dateNow是当前日期 
	    dateFirst是当年第一天  
	    dataNumber是当前日期是今年第多少天  
	    用dataNumber + 当前年的第一天的周差距的和在除以7就是本年第几周
	*/
  let dateNow = new Date(year, parseInt(month) - 1, date);
  let dateFirst = new Date(year, 0, 1);
  let dataNumber = Math.round((dateNow.valueOf() - dateFirst.valueOf()) / 86400000);
  return Math.ceil((dataNumber + (dateFirst.getDay() + 1 - 1)) / 7);
}

// 获取今天是星期几
function getCurrentWeekday() {
  const myDate = new Date();
  let days = myDate.getDay();
  const number = days;
  switch (days) {
    case 1:
      days = "周一";
      break;
    case 2:
      days = "周二";
      break;
    case 3:
      days = "周三";
      break;
    case 4:
      days = "周四";
      break;
    case 5:
      days = "周五";
      break;
    case 6:
      days = "周六";
      break;
    case 0:
      days = "周日";
      break;
  }
  return {
    number: number,
    weekday: days,
  };
}

// 获取今天的 年月日
function getCurrentYearMonthDay() {
  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return {
    year: year,
    month: month,
    day: day,
  };
}

// 时间格式化。可以作为过滤器直接使用。
/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string}
 */
function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
  const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}

// time ago，时间格式化。可以作为过滤器直接使用。
/**
 * @param {number|string|object} time
 * @param {string} option
 * @returns {string}
 */
function formatTime(time, option) {
  if (arguments.length === 0) {
    return null;
  }
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const d = date;
  const now = Date.now();

  const diff = (now - d) / 1000;

  if (diff < 30) {
    return "刚刚";
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + "分钟前";
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + "小时前";
  } else if (diff < 3600 * 24 * 2) {
    return "1天前";
  }
  if (option) {
    return parseTime(time, option);
  } else {
    return d.getMonth() + 1 + "月" + d.getDate() + "日" + d.getHours() + "时" + d.getMinutes() + "分";
  }
}

// 只能比较同一天之内的时间，不跨天比较
// 24小时制，最大时间23:59
function inTimePeriod(start, end) {
  const now = new Date();
  const nowH = now.getHours() * 1;
  const nowM = now.getMinutes() * 1;
  const _now = nowH * 60 + nowM;
  const startArr = start.split(":");
  const startH = startArr[0] * 1;
  const startM = startArr[1] * 1;
  const _start = startH * 60 + startM;
  const endArr = end.split(":");
  const endH = endArr[0] * 1;
  const endM = endArr[1] * 1;
  const _end = endH * 60 + endM;
  if (_now > _end) {
    // 已经过时
    return -1;
  }
  if (_now >= _start && _now <= _end) {
    // 正在进行
    return 0;
  }
  // 尚未开始
  return 1;
}
// 获取当前格式化后的时间日期;
function getNowFormatDate(date) {
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}
/*
	 startYear 开始年份 参数为空时为当前年份前5年
	 endYear 结束年份 参数为空时为当前年份
	*/
function getYearArr(startYear, endYear) {
  var yearArr = [],
    prDate = new Date(),
    presentYear = prDate.getFullYear(); //当前年份
  if (!endYear) {
    //为空为当前年份
    endYear = presentYear;
  }
  if (!startYear) {
    //为空为当前年前5年
    startYear = presentYear - 5;
  }
  for (var i = startYear; i <= endYear; i++) {
    yearArr.push(i);
  }
  return yearArr;
}
Vue.prototype.$date = {
  getYearArr,
  getNowFormatDate,
  inTimePeriod,
  formatTime,
  getCurrentYearMonthDay,
  getCurrentWeekday,
  getYearWeek,
  getMonthWeek,
  getCurrentWeekNumber,
  getDatesAfterWeeks,
  getWeekDatesForYMD,
  getDayWeekday,
  getWeekDates,
  getMonthDays,
  timeStrToTimestamp,
  getPastDatetime,
  getDateTimeNow,
  timestampToTime,
  timeNow,
  dateformat
};